package com.org.gundutubruk;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Point;
import android.util.Log;


public class GunduController
{
	private static final String TAG = GameView.class.getSimpleName();
	//titik apollo
	final Point Puncak;
	final Point Kiri_Atas;
	final Point Kiri_Bawah;
	final Point Kanan_Atas;
	final Point Kanan_Bawah;

	//titik buletan
	final Point pusatLingkaran;
	final int jarijari;
		
	public ArrayList<Gundu> Ball= new ArrayList<Gundu>();//variabel penampung semua nada drum		
	private Bitmap nonPlayer;
	public Bitmap gundubiasa;
	private Bitmap gundu1;
	private Bitmap gundu2;
	private Bitmap gundu3;
	private Bitmap gundu4;
	private Bitmap gundu5;
	public int gesekan[][];
	public final static int TANAH_BIASA = 5;
	public final static int AIR = 10;
	public final static int BATU = 30;
	public final static int RUMPUT = 25;
	public final static int LUMPUR = 40;
	//ukuran petak layar
	private int scale_x,scale_y;//lebar dan panjang satu sel(buat gesekan)
	private int width, height;//lebar dan panjang map
	
	//status hit
	public int hit1=-9999;
	public int hit2=-9999;
	
	//animasi cipratan air
	public static Bitmap[] animwater;
	//animasi cipratan batu
	public static Bitmap[] animStone;
	//animasi cipratan rumput
	public static Bitmap[] animGrass;
	//animasi cipratan lumpur
	public static Bitmap[] animMud;
	
	//animas marker
	public static Bitmap[] animMarker;
	
	Context context;
	
    public GunduController(Context context,Resources res, int _width, int _height, int mode, int taruhan){
    	width = _width;
    	height = _height;
    	nonPlayer = BitmapFactory.decodeResource(res, R.drawable.gundumotion1);
    	gundubiasa = BitmapFactory.decodeResource(res, R.drawable.gundumotion1);
    	gundu1 = BitmapFactory.decodeResource(res, R.drawable.gundu1);
    	gundu2 = BitmapFactory.decodeResource(res, R.drawable.gundu2);
    	gundu3 = BitmapFactory.decodeResource(res, R.drawable.gundu3);
    	gundu4 = BitmapFactory.decodeResource(res, R.drawable.gundu4);
    	gundu5 = BitmapFactory.decodeResource(res, R.drawable.gundu5);
    	
    	this.context = context;   
    	animwater = new Bitmap[5];
    	animwater[0] = BitmapFactory.decodeResource(res, R.drawable.water_1);
    	animwater[1] = BitmapFactory.decodeResource(res, R.drawable.water_2); 
    	animwater[2] = BitmapFactory.decodeResource(res, R.drawable.water_3);
    	animwater[3] = BitmapFactory.decodeResource(res, R.drawable.water_4);
    	animwater[4] = BitmapFactory.decodeResource(res, R.drawable.water_5);
    	animGrass = new Bitmap[5];
    	animGrass[0] = BitmapFactory.decodeResource(res, R.drawable.grass_1);
    	animGrass[1] = BitmapFactory.decodeResource(res, R.drawable.grass_2);
    	animGrass[2] = BitmapFactory.decodeResource(res, R.drawable.grass_3);
    	animGrass[3] = BitmapFactory.decodeResource(res, R.drawable.grass_4);
    	animGrass[4] = BitmapFactory.decodeResource(res, R.drawable.grass_5);
    	animMud = new Bitmap[5];
    	animMud[0] = BitmapFactory.decodeResource(res, R.drawable.mud_1);
    	animMud[1] = BitmapFactory.decodeResource(res, R.drawable.mud_2);
    	animMud[2] = BitmapFactory.decodeResource(res, R.drawable.mud_3);
    	animMud[3] = BitmapFactory.decodeResource(res, R.drawable.mud_4);
    	animMud[4] = BitmapFactory.decodeResource(res, R.drawable.mud_5);
    	animStone = new Bitmap[5];
    	animStone[0] = BitmapFactory.decodeResource(res, R.drawable.kerikil_1);
    	animStone[1] = BitmapFactory.decodeResource(res, R.drawable.kerikil_2);
    	animStone[2] = BitmapFactory.decodeResource(res, R.drawable.kerikil_3);
    	animStone[3] = BitmapFactory.decodeResource(res, R.drawable.kerikil_4);
    	animStone[4] = BitmapFactory.decodeResource(res, R.drawable.kerikil_5);
    	//load gambar marker
    	animMarker = new Bitmap[14];
    	animMarker[0] = BitmapFactory.decodeResource(res, R.drawable.c_1);
    	animMarker[1] = BitmapFactory.decodeResource(res, R.drawable.c_2);
    	animMarker[2] = BitmapFactory.decodeResource(res, R.drawable.c_3);
    	animMarker[3] = BitmapFactory.decodeResource(res, R.drawable.c_4);
    	animMarker[4] = BitmapFactory.decodeResource(res, R.drawable.c_5);
    	animMarker[5] = BitmapFactory.decodeResource(res, R.drawable.c_6);
    	animMarker[6] = BitmapFactory.decodeResource(res, R.drawable.c_7);
    	animMarker[7] = BitmapFactory.decodeResource(res, R.drawable.c_8);
    	animMarker[8] = BitmapFactory.decodeResource(res, R.drawable.c_9);
    	animMarker[9] = BitmapFactory.decodeResource(res, R.drawable.c_10);
    	animMarker[10] = BitmapFactory.decodeResource(res, R.drawable.c_11);
    	animMarker[11] = BitmapFactory.decodeResource(res, R.drawable.c_12);
    	animMarker[12] = BitmapFactory.decodeResource(res, R.drawable.c_13);
    	animMarker[13] = BitmapFactory.decodeResource(res, R.drawable.c_14);
    	// init apollo
    	Puncak = new Point(465,81);
    	Kiri_Atas = new Point(420,165);
    	Kiri_Bawah = new Point(420,549);
    	Kanan_Atas = new Point(510,165);
    	Kanan_Bawah = new Point(510,549);
    	pusatLingkaran = new Point(470,385);
    	jarijari = 160;
    	if (mode==1){ //buletan
    		Buletan(taruhan,140);
    	}
    	else if(mode==2){ //apollo
    		Apollo(taruhan);
    	}
    	else if(mode==4){ // latihan
    		Latihan();
    	}
    	MapExtern(res);
    }
    
    public void ChangeGundu(int id, int play){
    	if (FirstPlayer()!=-9999){
	    	if (play==1) {
	    		//markerin player
	    		Ball.get(id).marked = true;	    		
	    	}else{ 
	    		//nonmarkerin non player
	    		Ball.get(id).marked = false;	    		
	    	}
    	}
    }
    public boolean GunduExist(){
    	boolean ketemu = false;
    	if (Ball.size()!=0){
    		ketemu = true;
    	}
    	return ketemu;
    }
    public boolean noGunduBuletan(){
    	boolean truthy = false;
    	if ((!GunduExist()) || (FirstPlayer()==0)){
    		truthy = true;
    	}
    	return truthy;
    }
    public void PositioningApollo(){
    	for(int i=0;i<CountGunduNP();i++){
       		Ball.get(i).x = Puncak.x - (gundubiasa.getWidth()/2);
       		Ball.get(i).y = (int)(Kiri_Atas.y)+((i-1)*(gundubiasa.getHeight() + 10) - gundubiasa.getHeight()/2);       	
       	}
    }
    public int FurthestPlayer(){
    	int Max = Ball.size()-1;
		int i = Max-1;
		while (getGundu(i).isPlayer() != 0){
			if(getGundu(i).y>=getGundu(Max).y){
				Max = i;
			}
			i-=1;
		}
		return Max;
    }
    public boolean NoGunduMoving(){
    	boolean IsMove = true;
    	if(Ball.size()>0){
	    	for (int i=0;i<Ball.size();i++){
	    		if ((Ball.get(i).xinc!=0)||(Ball.get(i).yinc!=0)){
	    			IsMove = false;
	    		}
	    	}
    	}
    	return IsMove;
    }
    public int NearestPlayer(int akhir){ // mengembalikan indeks player yang jaraknya terpendek
    	int Min = akhir;
		int i = Min-1;
		while (getGundu(i).isPlayer() != 0){
			if(getGundu(i).getCenterY()<getGundu(Min).getCenterY()){
				Min = i;
			}
			i-=1;
		}
		return Min;
    }
    public int FirstPlayer(){ //mengembalikan indeks player terkecil
		int First = 0;
		boolean ketemu = false;
		First=0;
		while ((First!=Ball.size())&&(!ketemu)){
			if (getGundu(First).isPlayer() != 0){
				ketemu = true;
			}
			else{
				First+=1;
			}
		}
		if (!ketemu){
			First = -9999;
		}
		return First;
    }
    public void PositioningPlayer(){
    	int last = Ball.size()-1;
    	if (last>0){
	    	int min = NearestPlayer(last);
	    	int first = FirstPlayer();
	    	Gundu temp;
	    	while(last!=first){
	    		Log.d(TAG, "last = "+last+", min = "+min+", first="+first);   	
				temp = new Gundu(getGundu(min).getBitmap(), getGundu(min).isPlayer(), getGundu(min).mass, (int)getGundu(min).x, (int)getGundu(min).y, 0,0, Color.CYAN, width, height);
	    		Log.d(TAG, "temp = "+temp.isPlayer()+", min="+getGundu(min).isPlayer());
				Ball.remove(min);
				Ball.add(temp);   		
	    		last-=1;
	    		min = NearestPlayer(last);
	    	}
	    	temp = new Gundu(getGundu(min).getBitmap(), getGundu(min).isPlayer(), getGundu(min).mass, (int)getGundu(min).x, (int)getGundu(min).y, 0,0, Color.CYAN, width, height);
			Log.d(TAG, "temp = "+temp.isPlayer()+", min="+getGundu(min).isPlayer());
			Ball.remove(min);
			Ball.add(temp);
    	}
    }
    
    public int getIdPlayer(int player){ //ngembaliin indeks gundu dengan id player = player
    	int z=0;
    	while(getGundu(z).isPlayer()!=player){
    		z+=1;
    	}
    	if(getGundu(z).isPlayer()!=player){
    		z = -9999;
    	}
    	return z;
    }
    
    public void Info(){
    	for(int i=0;i<Ball.size();i++){
    		Log.d(TAG, "Bola "+i+" id="+Ball.get(i).isPlayer());
    	}
    }
    public void Buletan(int NumBall, int radius){//versi main buletan
    	int remaining = NumBall;    	
    	double wx, wy, distance;
    	Random randomGenerator = new Random();
    	// ini untuk starter buletan   		
       	do{    		
    		wx = randomGenerator.nextInt((jarijari - gundubiasa.getWidth()/2) - (-jarijari + gundubiasa.getWidth()/2) + 1) + -jarijari + gundubiasa.getWidth()/2;
    		wy = randomGenerator.nextInt((jarijari - gundubiasa.getWidth()/2) - (-jarijari + gundubiasa.getWidth()/2) + 1) + -jarijari + gundubiasa.getWidth()/2;       	
	    	distance=Math.sqrt(wx*wx+wy*wy);
	    	if ((!Tubrukan((int)wx+pusatLingkaran.x,(int)wy+pusatLingkaran.y))&&(distance<(radius - gundubiasa.getWidth()/2))){
    	    	Ball.add(new Gundu(randomizeGunduBitmapVariation(),0,300,(int)(wx+pusatLingkaran.x),(int)(wy + pusatLingkaran.y),0,0,Color.CYAN, width, height));
    			remaining-=1;
    		}
    	}while((remaining!=0));
    }
    public void Latihan(){//versi main Latihan
    	int remaining = 10;	
    	double wx, wy;
    	Random randomGenerator = new Random();
    	// ini untuk starter buletan   		
		Log.d(TAG, "jumlah gundu sekarang = "+Ball.size());
       	do{    		
    		wx = randomGenerator.nextInt(width);
    		wy = randomGenerator.nextInt(height);       	
	    	if (!Tubrukan((int)wx,(int)wy)){
    	    	Ball.add(new Gundu(randomizeGunduBitmapVariation(),0,300,(int)wx,(int)wy,0,0,Color.CYAN, width, height));
    			remaining-=1;
    		}
    	}while((remaining!=0));
		wx = randomGenerator.nextInt(width);
		wy = randomGenerator.nextInt(height);       	
    	while(FirstPlayer()==-9999){
    		wx = randomGenerator.nextInt(width);
    		wy = randomGenerator.nextInt(height);       	
    		if (!Tubrukan((int)wx,(int)wy)){
    		InsertPlayer
				(1,
				0, 0, (int)wx, (int)wy);							
        	}
    	}
    }
    public boolean Tubrukan(int center_x, int center_y){
    	boolean kebenaran = false;
    	if(Ball.size()>0){
	    	for (int i=0;i<Ball.size();i++){
	    		
	    		double wx=Ball.get(i).getCenterX()-center_x;
	    		double wy=Ball.get(i).getCenterY()-center_y;
		    	//colliding balls (theorem of Pythagoras)
		    	double distance=Math.sqrt(wx*wx+wy*wy);
		    	if(distance<Gundu.diameter){
		    		kebenaran = true;
		    	}
	    	}
    	}
    	return kebenaran;
    	
    }
    public void Apollo(int NumBall){//versi main uleran
    	// ini untuk starter apollo       	
       	for(int i=0;i<NumBall;i++){        	
       		Log.d(TAG,"coba spawn " + i + " sukses");
       		Ball.add(new Gundu(randomizeGunduBitmapVariation(),0,300,Puncak.x - (gundubiasa.getWidth()/2),(int)(Kiri_Atas.y)+((i-1)*(gundubiasa.getHeight() + 10) - gundubiasa.getHeight()/2),0,0,Color.CYAN, width, height));       	
       	}
    }
    public void InsertPlayer(int id, int powerx, int powery, int posisiX, int posisiY){//0 = nonplayer, sisanya player
    	Ball.add(new Gundu(nonPlayer,id,300,posisiX,posisiY,powerx,powery,Color.BLUE, width, height));   		
    }
    public Gundu getGundu(int i){
        return Ball.get(i);
    }
    public void update(){
    	for (int i=0;i<Ball.size();i++){
    		//Log.d(TAG, "jumlah gundu di update = "+Ball.size());
    		//Log.d(TAG,"kordinat ("+(int) ((Ball.get(i).x)/scale_x)+","+(int) ((Ball.get(i)).y/scale_y)+")"+i+". gesekan="+hitungGesekan(i));    	
    		Ball.get(i).move(hitungGesekan(i));
        }
        handleCollision();
    }
    public int hitungGesekan(int i){         	
    	return gesekan[(int) ((Ball.get(i).x)*1.0/(width/scale_x))][(int) ((Ball.get(i)).y*1.0/(height/scale_y))];
    }
    public void MapExtern(Resources Res){
    	Scanner read = new Scanner(Res.openRawResource(MapSystem.getFrictionMap(GameSettingHolder.playMap)));
    	String s;
    	if(read.hasNext()) {
			//Log.d(TAG,s);			
    		s = read.next();
    		Log.d(TAG,"berapa"+Integer.valueOf(s));			
    		scale_x = Integer.valueOf(s);
    		s = read.next();
    		scale_y = Integer.valueOf(s);
    		Log.d(TAG,"kali"+Integer.valueOf(s));		
    		
        	gesekan = new int[scale_x][scale_y];

			for(int i=0;i<scale_y;i++) {
				s = read.next();
				for(int j=0;j<scale_x;j++) {
					if(s.charAt(j)=='0') {//tanah biasa / permukaan biasa
						gesekan[j][i] = TANAH_BIASA;
					}
					else if (s.charAt(j)=='1'){// air
		        		gesekan[j][i] = AIR;        		
					}
					else if (s.charAt(j)=='2'){// batu
		        		gesekan[j][i] = BATU;        		
					}
					else if (s.charAt(j)=='3'){// rumput
		        		gesekan[j][i] = RUMPUT;        		
					}
					else if(s.charAt(j)=='4') {// lumpur
						gesekan[j][i] = LUMPUR;
					}
				}
			}			
		}else{
		}
    }
    public int CountGunduNP(){
    	int jumlah=0;
    	if (GunduExist()){
    		for(int z=0;z<Ball.size();z++){
    			if  (getGundu(z).isPlayer()==0){
    				jumlah+=1;
    			}
    		}
    	}
    	return jumlah;
    }
    boolean collide(Gundu b1, Gundu b2){
    	double wx=b1.getCenterX()-b2.getCenterX();
    	double wy=b1.getCenterY()-b2.getCenterY();
    	double dx, dy, collisionision_angle, magnitude_1, magnitude_2, direction_1, direction_2, new_xspeed_1, new_yspeed_1, new_xspeed_2, new_yspeed_2, final_xspeed_1, final_xspeed_2, final_yspeed_1, final_yspeed_2;
    	//colliding balls (theorem of Pythagoras)
    	double distance=Math.sqrt(wx*wx+wy*wy);
    	if(distance<Gundu.diameter){
    		if (b1.x<b2.x){
    			b1.x-=1;//(30-distance)/4;
    			b2.x+=1;//(30-distance)/4;
    		}
    		else {
    			b1.x+=1;//(30-distance)/4;
    			b2.x-=1;//(30-distance)/4;
    		}
    		if (b1.y<b2.y){
    			b1.y-=1;//(30-distance)/4;
    			b2.y+=1;//(30-distance)/4;
    		}
    		else {
    			b1.y+=1;//(30-distance)/4;
    			b2.y-=1;//(30-distance)/4;
    		}
    		dx = b1.getCenterX()- b2.getCenterX();
    		dy =  b1.getCenterY()- b2.getCenterY();
    		collisionision_angle = Math.atan2(dy, dx);
    		magnitude_1 = Math.sqrt(b1.xinc * b1.xinc + b1.yinc * b1.yinc);
    		magnitude_2 = Math.sqrt(b2.xinc * b2.xinc + b2.yinc*b2.yinc);
    		direction_1 = Math.atan2(b1.yinc, b1.xinc);
    		direction_2 = Math.atan2(b2.yinc, b2.xinc);
    		new_xspeed_1 = magnitude_1 * Math.cos(direction_1 - collisionision_angle);
    		new_yspeed_1 = magnitude_1 * Math.sin(direction_1 - collisionision_angle);
    		new_xspeed_2 = magnitude_2 * Math.cos(direction_2 - collisionision_angle);
    		new_yspeed_2 = magnitude_2 * Math.sin(direction_2 - collisionision_angle);
    		final_xspeed_1 = ((b1.mass - b2.mass) * new_xspeed_1 + (b2.mass + b2.mass) * new_xspeed_2) / (b1.mass + b2.mass);
    		final_xspeed_2 = ((b1.mass + b1.mass) * new_xspeed_1 + (b2.mass - b1.mass) * new_xspeed_2) / (b1.mass + b2.mass);
    		final_yspeed_1 = new_yspeed_1;
    		final_yspeed_2 = new_yspeed_2;
    		b1.xinc = Math.cos(collisionision_angle) * final_xspeed_1 + Math.cos(collisionision_angle + 1.570796E+000) * final_yspeed_1;
    		b1.yinc = Math.sin(collisionision_angle) * final_xspeed_1 + Math.sin(collisionision_angle + 1.570796E+000) * final_yspeed_1;
    		b2.xinc = Math.cos(collisionision_angle) * final_xspeed_2 + Math.cos(collisionision_angle + 1.570796E+000) * final_yspeed_2;
    		b2.yinc = Math.sin(collisionision_angle) * final_xspeed_2 + Math.sin(collisionision_angle + 1.570796E+000) * final_yspeed_2;
    		b1.xinc = b1.xinc*0.98;
    		b1.yinc = b1.yinc*0.98;
    		b2.xinc = b2.xinc*0.98;
    		b2.yinc = b2.yinc*0.98;
    		return true;
    	}
    	return false;
    }
    public int CountGunduOut(){
    	int Jumlah=0;
    	int temp=0;
    	double wx;
    	double wy;
    	//colliding balls (theorem of Pythagoras)
    	double distance;
    	while (getGundu(temp).isPlayer()==0){
    		wx=pusatLingkaran.x-getGundu(temp).getCenterX();
    		wy=pusatLingkaran.y-getGundu(temp).getCenterY();
    		distance=Math.sqrt(wx*wx+wy*wy);
    		if (distance>(jarijari)){ // lebih dari radius, gundu dihapus
    			Jumlah+=1;
    		}
    		temp+=1;
    	}
    	Log.d(TAG,"jumlah gundu di luar = "+Jumlah);
    	return Jumlah;
    }
    public void RemoveGunduOut(){    	
    	int temp=0;
    	double wx;
    	double wy;
    	//colliding balls (theorem of Pythagoras)
    	double distance;
    	while (getGundu(temp).isPlayer()==0){    		
    		wx=(pusatLingkaran.x)-getGundu(temp).getCenterX();
    		wy=(pusatLingkaran.y)-getGundu(temp).getCenterY();
    		distance=Math.sqrt(wx*wx+wy*wy);
    		if (distance>(jarijari)){ // lebih dari radius dihapus
    			Ball.remove(temp);
    			Log.d(TAG,"hapus gundu ke = "+temp);
    		}
    		else{
    			temp+=1;
    		}
    	}    	
    }
    
    
    public boolean BallinApollo(Gundu B){
    	boolean aman = false;
    	int kiri, kanan, atas, bawah;
		kiri = Kiri_Atas.x;
		kanan = Kanan_Atas.x;
		atas = Puncak.y;
		bawah = Kanan_Bawah.y;
		Coordinate a = new Coordinate(Kiri_Atas.x, Kiri_Atas.y);
		Coordinate b = new Coordinate(Kanan_Atas.x, Kanan_Atas.y);
		Coordinate c = new Coordinate(Puncak.x, Puncak.y);
		Coordinate d = new Coordinate(B.x, B.y);
		
		if(((B.x<=kanan)&&(B.x>=kiri)&&(B.y>=atas)&&(B.y<=bawah)) || (Utility.checkPointInsideTriangle(a, b, c, d))){
			aman = true;
		}
		
    	return aman;
    }

    public int CheckApolloNP(){
    	int retval = -9999;
    	if(CountGunduNP()>0){
        	Log.d(TAG, "Count Gundu NP="+CountGunduNP());
    		for(int z=0;z<CountGunduNP();z++){
    			if ((retval==(-9999))&&(!BallinApollo(getGundu(z)))){
    				retval = z;
    			}
    		}
    	}
    	return retval;
    }
    public void PutBackPlayer(){
		for(int z=0;z<Ball.size();z++){
			if ((Ball.get(z).y<165) && (Ball.get(z).isPlayer()!=0)){
				Ball.get(z).y = (height/3)+650;
			}
		}
    }
    public int CheckApolloP(){
    	int retval=-9999;
    	if((Ball.size()-CountGunduNP())>0){        	
    		Log.d(TAG, "First Player = "+FirstPlayer());
    		for(int z=FirstPlayer();z<Ball.size();z++){
    			if ((retval==(-9999))&&(BallinApollo(getGundu(z)))){
    				retval = z;
    			}
    		}
    	}
    	return retval;
    }
    public int CheckPlayer(){
    	int Jumlah=0;
    	int temp=FirstPlayer();
    	double wx;
    	double wy;
    	//colliding balls (theorem of Pythagoras)
    	double distance;
		while (temp!=Ball.size()){
    		wx=pusatLingkaran.x-getGundu(temp).getCenterX();
    		wy=pusatLingkaran.y-getGundu(temp).getCenterY();
    		distance=Math.sqrt(wx*wx+wy*wy);
    		if (distance<=(jarijari)){ //+radius
    			Ball.remove(temp);
    			Log.d(TAG,"hapus gundu ke = "+temp);
    			Jumlah+=1;
    		}
    		else{
    			temp+=1;
    		}
    	}
		return Jumlah;
    }
    public int PlayerOut(){
    	int Indeks=FirstPlayer();
    	boolean ketemu = false;
    	double wx;
    	double wy;
    	//colliding balls (theorem of Pythagoras)
    	double distance;
    	if(Indeks!=-9999){
			while ((Indeks!=Ball.size())&&(!ketemu)){
				Log.d(TAG, "indeks = "+Indeks);
				wx = pusatLingkaran.x-getGundu(Indeks).getCenterX();
				wy = pusatLingkaran.y-getGundu(Indeks).getCenterY();
	    		distance=Math.sqrt(wx*wx+wy*wy);
	    		if (distance<=(jarijari)){ //+radius
	    			ketemu = true;
	    			Log.d(TAG, "KETEMU -9999 di = "+Indeks);
	    		}else{
	    			Indeks+=1;
	    		}
	    	}
			if (!ketemu){
				Indeks = -9999;
			}
    	}
		return Indeks;
    }
    public int GunduOutOfBuletan(){
    	double wx;
    	double wy;
    	int z=-9999;
    	//colliding balls (theorem of Pythagoras)
    	double distance;
    	boolean ketemu = false;
    	if (CountGunduNP()>0){
    		z=0;
    		while((z!=CountGunduNP())&&(!ketemu)){
	    		wx=(pusatLingkaran.x)-getGundu(z).getCenterX();
	    		wy=(pusatLingkaran.y)-getGundu(z).getCenterY();
	    		distance=Math.sqrt(wx*wx+wy*wy);
	    		if (distance>(jarijari)){ //+radius
	    			ketemu = true;
	    			Log.d(TAG, "KETEMU di = "+z);
	    			Log.d(TAG, "kordinat x= "+getGundu(z).getCenterX()+", kordinat y = "+getGundu(z).getCenterY());
	    		}else{
	    			z+=1;
	    		}
    		}
    		if (!ketemu){
    			z = -9999;
    		}
    	}
    	return z;
    }
    
    public boolean PlayerInBuletan(int id){
    	boolean radius = false;
    	double wx;
    	double wy;
    	//colliding balls (theorem of Pythagoras)
    	double distance;
  		wx=(pusatLingkaran.x)-getGundu(id).getCenterX();
   		wy=(pusatLingkaran.y)-getGundu(id).getCenterY();
  		distance=Math.sqrt(wx*wx+wy*wy);
   		if (distance<=(jarijari)){ //+radius
   			radius = true;
   			Log.d(TAG, "KETEMU radius");
  		}
   		return radius;
    }
    
    private void handleCollision(){
    	Gundu temp;
    	//we iterate through all the balls, checking for collision
    	for(int i=0;i<Ball.size();i++){
    		for(int j=0;j<Ball.size();j++){
    			if(i!=j)
    			{
    				if(collide(Ball.get(i), Ball.get(j))){    					
    					GameSound.startTabrakan(context);    					
    					hit1 = i;
    					hit2 = j;
    					temp = Ball.get(i);
    					Ball.get(i).hit(Ball.get(j));
    					Ball.get(j).hit(temp);
    				}
    			}
    		}
        }
    }
    
    //ngerandom variasi gundu yang muncul di layar hape
    //seberapa banyak variasi gundu yang dimunculin tergantung unlock level terbesar player-player yang sedang main
    private Bitmap randomizeGunduBitmapVariation() {
    	Random random = new Random();
    	int maxVar = 0;
    	//maksimum variasi adalah maksimum variasi dari seluruh player
    	for(int i=0;i<GameSettingHolder.PlayingPlayer.size();i++) {
    		if(maxVar < GameSettingHolder.PlayingPlayer.get(i).unlockLevel) {
    			maxVar = GameSettingHolder.PlayingPlayer.get(i).unlockLevel;
    		}
    	}
    	int hasilVarGundu = random.nextInt(maxVar+1);
    	switch(hasilVarGundu) {
    	case 0:
    		return gundubiasa;
    	case 1:
    		return gundu1;
    	case 2:
    		return gundu2;
    	case 3: 
    		return gundu3;
    	case 4:
    		return gundu4;
    	case 5:
    		return gundu5;
    	default:
    		return gundubiasa;
    	}
    }
}
